﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Filter;
    using Serotonin.Controls;
    using System;
    using System.Drawing;
    using System.Globalization;
    using System.Windows.Forms;

    /// <summary>Filter specified properties form.</summary>
    public partial class OilPaintingForm : Form, IOptionsForm
    {
        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        private readonly OilPainting filter;

        /// <summary>
        /// Initializes a new instance of the <see cref="ColoredCannyForm"/> class.
        /// </summary>
        /// <param name="processor">The SBIP processor.</param>
        public OilPaintingForm(Processor processor)
        {
            InitializeComponent();
            filter = AOF.Init(processor, this, gbProcess,
             new OilPainting { BrushSize = trBarDiameter.Value }) as OilPainting;

// ReSharper disable PossibleNullReferenceException
            lbDiameter.Text = filter.BrushSize.ToString(CultureInfo.InvariantCulture);
// ReSharper restore PossibleNullReferenceException
                
        }

        /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            Bitmap b = filter.Apply(Processor.OriginalImage);

            string s = Program.L.Text[40] + Program.L.Text[7] + ": " +
            gbBrushSize.Text + "-" + lbDiameterText.Text + "=" + 
            lbDiameter.Text + "; ";

            Processor.Change(b, s); 
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[7];
            tabctrlOptions.TabPages[0].Text = Program.L.Text[57];
            gbBrushSize.Text = Program.L.Text[66];
            lbDiameterText.Text = Program.L.Text[67];
        }

        private void TrBarDiameterValueChanged(object sender, EventArgs e)
        {
            filter.BrushSize = trBarDiameter.Value;// * 2 + 1);
            lbDiameter.Text = filter.BrushSize.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {
            EndlessProgressBarFormInThread form =
                new EndlessProgressBarFormInThread(
                    Program.L.Text[63],
                    Program.L.Text[64]);
            form.Start();

            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }

            form.End();
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }
    }
}
